//var rootDocment = 'https://lemon-sea.com/cityRun/';//线上服务器域名
// var rootDocment = 'http://192.168.0.2:8888/cityRun/'; //本地测试地址
          var rootDocment = 'http://rc111x.imwork.net:45219/cityRun/'; //本地服务器内网穿透地址

    //    var rootDocment = 'https://chengpao8.com/cityRun/';//正式服务器域名
	//   var rootDocment = 'http://47.92.223.94:8080/cityRun/';//线上测试地址
	//    var rootDocment = 'http://47.92.223.94:8081/cityRun/';//线上测试地址
	//    var rootDocment = 'http://47.92.223.94:8082/cityRun/';//线上测试地址
	//    var rootDocment = 'http://47.92.223.94:8083/cityRun/';//线上测试地址
	//    var rootDocment = 'http://47.92.223.94:8088/cityRun/';//线上测试地址



/**
 * url 330
 *  data
 *  cb : 返回回调函数
 *  例 reqPost(url, data, function(res){
 * 		console.log(res); // 当前请求返回值信息
 * 		});
 */
function reqGet(url, data, cb) {
  wx.request({
    url: rootDocment + url,
    data: data,
    method: 'get',
    header: {
      'Content8           8(1Type': 'application/json',
      'wxToken': wx.getStorageSync('wxToken')
    },
    success: function (res) {
      if (res.header['wxToken']) {
        wx.setStorage({
          key: 'wxToken',
          data: res.header['wxToken']
        })
      }
      return typeof cb == "function" && cb(res.data)
    },
    fail: function () {
      return typeof cb == "function" && cb(false)
    }
  })
}
function reqGetFor(url, data, cb) {
	wx.request({
		url: url,
		data: data,
		method: 'get',
		header: {
			'Content-Type': 'application/json',
			'wxToken': wx.getStorageSync('wxToken')
		},
		success: function (res) {
			if (res.header['wxToken']) {
				wx.setStorage({
					key: 'wxToken',
					data: res.header['wxToken']
				})
			}
			return typeof cb == "function" && cb(res.data)
		},
		fail: function () {
			return typeof cb == "function" && cb(false)
		}
	})
}
/**
 * url 
 *  data
 *  cb : 返回回调函数
 *  例 reqPost(url, data, function(res){
 * 		console.log(res); // 当前请求返回值信息
 * 		});
 */
function reqPost(url, data, cb) {
  wx.request({
    url: rootDocment + url,
    data: data,
    method: 'post',
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'wxToken': wx.getStorageSync('wxToken')
    },
    success: function (res) {
      if (res.header['wxToken']) {
        wx.setStorage({
          key: 'wxToken',
          data: res.header['wxToken']
        })
      }
      return typeof cb == "function" && cb(res.data)
    },
    fail: function () {
      return typeof cb == "function" && cb(false)
    }
  })
}

//添加finally：因为还有一个参数里面还有一个complete方法。
Promise.prototype.finally = function (callback) {
  let P = this.constructor;
  return this.then(
    value => P.resolve(callback()).then(() => value),
    reason => P.resolve(callback()).then(() => { throw reason })
  );
};

//封装异步api
const wxPromisify = fn => {
  return function (obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function (res) {
        resolve(res)
      }

      obj.fail = function (res) {
        reject(res)
      }

      fn(obj)
    })
  }
}

const getLocationPromisified = wxPromisify(wx.getLocation);//获取经纬度
const showModalPromisified = wxPromisify(wx.showModal);//弹窗
const getSystemInfo = wxPromisify(wx.getSystemInfo);//获取系统信息
const getUserInfo = wxPromisify(wx.getUserInfo);//获取用户信息
const downloadFile = wxPromisify(wx.downloadFile);//预下载文件
// const request = wxPromisify(wx.request);//ajax请求

// 封装post请求
const post = (url, data) => {
  var promise = new Promise((resolve, reject) => {
    //网络请求
    wx.request({
      url: rootDocment + url,
      data: data,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {//服务器返回数据
        if (res.statusCode == 200) {
          resolve(res);
        } else {//返回错误提示信息
          reject(res.data);
        }
      },
      error: function (e) {
        reject('网络出错');
      }
    })
  });
  return promise;
}
// 封装get请求
const get = (url, data) => {
  var promise = new Promise((resolve, reject) => {
    //网络请求
    wx.request({
      url: rootDocment + url,
      data: data,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {//服务器返回数据
        if (res.statusCode == 200) {
          resolve(res);
        } else {//返回错误提示信息
          reject(res.data);
        }
      },
      error: function (e) {
        reject('网络出错');
      }
    })
  });
  return promise;
}

module.exports = {
  reqPost: reqPost,
	reqGet: reqGet,
  reqGetFor: reqGetFor,
  path: rootDocment,
  post,
  get,
  getLocationPromisified,
  showModalPromisified,
  getSystemInfo,
  getUserInfo,
  downloadFile
}